<?php $this->_extends('../_layouts/default_layout'); ?>
<?php $this->_block('contents'); ?>
<link type="text/css" href="<?=Q::ini('custom_system/base_url')?>css/jq.autocomplete.css" rel="stylesheet" />
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/jq.autocomplete.js"></script>
<script type="text/javascript">
Refresh = function(){
	var wid = $("#warehouse_id").val();
	$(".ajax_product").autocomplete("<?=url('Purchase::Order/ProductInfoAjax')?>&warehouse_id="+wid).result(function(event, data, formatted){
		if (data[0])
		{
			var container = $(this).parent().parent().parent();
			container.find("input.product_number").val(data['number']);
			container.find("input[name$='product_basic_id]']").val(data['id']);
			container.find("input.safe_quantity").val(data['safe_quantity']);
			container.find("input.warning_quantity").val(data['warning_quantity']);
			container.find("input.budget_price").val(data['budget_price']);
			container.find("input.actual_quantity").val(data['actual_quantity']);
			container.find("input.sold_quantity").val(data['sold_quantity']);
			container.find("input.purchase_budget_quantity").val(data['purchase_budget_quantity']);
			container.find("input.purchase_quantity").val(data['purchase_quantity']);
			container.find("input.wholesale_quantity").val(data['wholesale_quantity']);
			container.find("input.sales_status").val(data['sales_status']);
			container.find("input.purchase_status").val(data['purchase_status']);			
			container.find("input.budget_provider_id").val(data['preferred_provider_id']);
			container.find("input.ajax_provider").val(data['preferred_provider_name']);
			container.find("input.purchase_price").val(data['purchase_price']);
		}
		$("input.ajax_product").focus(function(){if ($(this).val().length*1.6 > 32){$(this).attr("size",$(this).val().length*1.8);}});
		$("input.ajax_product").keyup(function(){if ($(this).val().length*1.6 > 32){$(this).attr("size",$(this).val().length*1.8);}});
	});
	$(".ajax_provider").autocomplete("<?=url('Purchase::Order/Provider')?>").result(function(event, data, formatted){
		if (data[0])
		{
			var container = $(this).parent().parent().parent();
			container.find("input.budget_provider_id").val(data['id']);
		}
	});
};
$(function(){
	//日期
	$(".datepicker").datepicker({dateFormat:'yy-mm-dd',changeMonth:true});
	//删除按钮
	$(".delete").live("click", function(){
		$(this).parent().parent().parent().parent().remove();
	});
	$("#warehouse_id").change(function(){
		$("#item_list").children().remove();
	});
	var i = "<?=isset($order['item']) ? count($order['item']) : 0 ?>";
	$("#add_item").click(function(){
		var _html = "<table width='100%' class='list_table'>";
		_html += "<tr><td align='left' colspan='6'><label><input type='checkbox' class='is_checked' checked='checked' /> <span>产品编号</span></label>";
		_html += " <input type='text' size='6' disabled='disabled' class='product_number' />";
		_html += " <input type='text' size='32' class='ajax_product' />";
		_html += "<input type='hidden' name='item[new_" + i + "][product_basic_id]' />";
		_html += " 警告库存 <input type='text' style='border:none' size='3' class='warning_quantity'  />";
		_html += " 安全库存 <input type='text' style='border:none' size='3' class='safe_quantity' />";
		_html += " 库存 <input type='text' style='border:none' size='3' class='actual_quantity' />";
		_html += " 待发 <input type='text' style='border:none' size='3' class='sold_quantity' />";
		_html += " 录单数 <input type='text' style='border:none' class='purchase_budget_quantity' size='3' />";
		_html += " 采购数 <input type='text' style='border:none' class='purchase_quantity' size='3' />";
		_html += " 批发数 <input type='text' style='border:none' class='wholesale_quantity' size='3' />";
		_html += " 采购状态 <input type='text' style='border:none' class='purchase_status' size='8' />";
		_html += " 销售状态 <input type='text' style='border:none' class='sales_status' size='8' />";
		_html += " 资料成本 <input type='text' style='border:none' class='purchase_price' size='8' /></td></tr>";
		_html += "<tr><td width='130'>预计采购数 <input type='text' size='5' name='item[new_" + i + "][budget_quantity]' /></td>";
		_html += "<td width='130'>预计采购价 <input type='text' size='5' name='item[new_" + i + "][budget_price]' class='budget_price' /></td>";
		_html += "<td width='300'>预计供应商 <input type='text' size='4' disabled='disabled' class='budget_provider_id' /> <input type='text' class='ajax_provider' /> <input type='hidden' class='budget_provider_id' name='item[new_" + i + "][budget_provider_id]' /></td>";
		_html += "<td>原因 <select name='item[new_" + i + "][reason]'><option value=''>请选择..</option><?foreach (Q::ini('custom_flag/purchase_order_item_reason') as $por):?><option value=<?=$por['value']?>><?=$por['name']?></option><?endforeach;?></select></td>"
		_html += "<td>备注 <input type='text' size='40' name='item[new_" + i + "][notes]' /></td>";
		_html += "<td><a class='delete'>删除</a></td></tr></table>";
		$("#item_list").append(_html);
		Refresh();
		i++;
	});
	
	//保存采购单
	$("#add_save").click(function(){
		var can_save = 1;
		//检查item产品
		$("#item_list").find("input[name$='product_basic_id]']").each(function(){
			if (!$(this).val())
			{
				can_save=0;
				alert("请检查每个item产品编号");
				return false;
			}
		});
		//检查Item预计采购数
		$("#item_list").find("input[name$='budget_quantity]']").each(function(){
			if (!$(this).val())
			{
				can_save=0;
				alert("请检查每个item预计采购数");
				return false;
			}
		});
		//检查item预计采购价
		$("#item_list").find("input[name$='budget_price]']").each(function(){
			if (!$(this).val())
			{
				can_save=0;
				alert("请检查每个item预计采购价");
				return false;
			}
		});
		//检查预计供应商
		$("#item_list").find("input[name$='budget_provider_id]']").each(function(){
			if (!$(this).val())
			{
				can_save=0;
				alert("请检查每个item预计供应商");
				return false;
			}
		});
		//检测是否有重复SKU
		var icp = $("input.product_number"), repeat = false;
		icp.each(function(){
			if (icp.filter('[value="' + this.value + '"]').size()>1)
			{
				repeat = this.value;
			}
		});
		if (repeat) 
		{
			if(!confirm('有重复的产品：' + repeat + '！是否继续操作？'))
			{
				return false;
			}
		}
		//检测是否需要提交数据
		$("#item_list").find("input.is_checked").each(function(){
			if (!$(this).is(":checked"))
			{
				$(this).parent().parent().parent().parent().remove();
			}
		});
		if (can_save)
		{
			$("form").submit();
			$(this).attr('disabled', true);
		}
	});
	Refresh();
});
</script>
<form method="post" action="<?=url('Purchase::Order/CreateSave')?>">
<table class="form_table">
	<tr>
		<th>采购日期</th>
		<td><input type="text" name="purchase_time" value="<?=isset($order['purchase_time']) ? $order['purchase_time'] : date('Y-m-d', CURRENT_TIMESTAMP)?>" class="datepicker" /></td>
		<th>采购人员</th>
		<td>
			<select name="purchase_user_id">
				<? foreach ($user as $u):?>
				<option value="<?=$u['id']?>" <?if ($u['id'] == CURRENT_USER_ID):?> selected="selected" <?endif;?>><?=$u['name']?></option>
				<? endforeach;?>
			</select>
		</td>
	</tr>
	<tr>
		<th>结款类型</th>
		<td>
			<select name="purchase_order_payment_type">
				<? foreach (Q::ini('custom_flag/purchase_order_payment_type') as $pt):?>
				<option value="<?=$pt['value']?>" <?if (isset($order['payment_type']) && $order['payment_type'] == $pt['value'])echo "selected='selected'" ?>><?=$pt['name']?></option>
				<? endforeach;?>
			</select>
		</td>
		<th>所在仓库</th>
		<td>
			<select name="warehouse_id" id="warehouse_id">
				<?foreach ($warehouse as $w):?>
				<option value="<?=$w['id']?>" <?if (isset($order['warehouse_id']) && $order['warehouse_id'] == $w['id'])echo "selected='selected'"?><?if (isset($_GET['warehouse_id']) && $_GET['warehouse_id'] == $w['id'])echo "selected='selected'"?>><?=$w['name']?></option>
				<?endforeach;?>
			</select>
		</td>
	</tr>
	<tr>
		<th>备注</th>
		<td colspan="3"><textarea rows="3" cols="60" name="notes"><?=isset($order['notes']) ? $order['notes'] : ''?></textarea></td>
	</tr>
</table>
<div id="item_list">
<?if(isset($order['item']) && $order['item'])foreach ($order['item'] as $k => $oi):?>
<table width="100%" class="list_table">
	<?$product_warehouse_ids = array();?>
	<?if (isset($_GET['product_warehouse_ids'])) $product_warehouse_ids = explode(',', $_GET['product_warehouse_ids']);?>
	<tr <? if (isset($_GET['product_warehouse_ids']) && in_array($oi['product_warehouse_id'], $product_warehouse_ids)):?>bgcolor="#ff8800"<?endif;?>>
		<td align="left" colspan="6">
			<label><input type="checkbox" class="is_checked" value="<?=isset($oi['product_warehouse_id']) ? $oi['product_warehouse_id'] : ''?>" <?if (isset($_GET['product_warehouse_ids']) && in_array($oi['product_warehouse_id'],$product_warehouse_ids))echo "checked='checked'" ?> />
			<span>产品编号</span></label>
			<input type="text" size="6" disabled="disabled" class="product_number" value="<?=isset($oi['number']) ? $oi['number'] : ''?>" />
			<input type="text" size="<?=strlen($oi['name'])?>" class="ajax_product" value="<?=isset($oi['name']) ? (isset($oi['number']) ? $oi['number'] : '').'&nbsp;'.htmlspecialchars($oi['name']) : '' ?>"/>
			<input type="hidden" name="item[<?=$k?>][product_basic_id]" value="<?=isset($oi['product_basic_id']) ? $oi['product_basic_id'] : ''?>"/>
			警告库存 <input type="text" style="border:none" size="3" class="warning_quantity" value="<?=isset($oi['warning_quantity']) ? $oi['warning_quantity'] : ''?>" />
			安全库存 <input type="text" style="border:none" size="3" class="safe_quantity" value="<?=isset($oi['safe_quantity']) ? $oi['safe_quantity'] : ''?>" />
			库存 <input type="text" style="border:none" size="3" class="actual_quantity" value="<?=isset($oi['actual_quantity']) ? $oi['actual_quantity'] : ''?>" />
			待发 <input type="text" style="border:none" size="3" class="sold_quantity" value="<?=isset($oi['sold_quantity']) ? $oi['sold_quantity'] : ''?>"/>
			录单数 <input type="text" style="border:none" size="3" value="<?=isset($oi['purchase_budget_quantity']) ? $oi['purchase_budget_quantity'] : ''?>" />
			采购数 <input type="text" style="border:none" class="purchase_quantity" size="3" value="<?=$oi['purchase_quantity']?>" />
			批发数 <input type="text" style="border:none" class="wholesale_quantity" size="3" value="<?=$oi['wholesale_quantity']?>" />
			采购状态 <input type="text" style="border:none" class="purchase_status" size="8" value="<?=Helper_BSS_Normal::getIniOne('product_purchase_status','value',$oi['purchase_status'],'name')?>" />
			销售状态 <input type="text" style="border:none" class="sales_status" size="8" value="<?=Helper_BSS_Normal::getIniOne('product_sales_status','value',$oi['sales_status'],'name')?>" />
			资料成本 <input type="text" style="border:none" class="purchase_price" size="8" value="<?=isset($oi['purchase_price']) ? $oi['purchase_price'] : ''?>" />
		</td>
	</tr>
	<tr <? if (isset($_GET['product_warehouse_ids']) && in_array($oi['product_warehouse_id'], $product_warehouse_ids)):?>bgcolor="#ff8800"<?endif;?>>
		<td width="130">预计采购数 <input type="text" size="5" name="item[<?=$k?>][budget_quantity]" value="<?=isset($oi['budget_quantity']) ? $oi['budget_quantity'] : ''?>" /></td>
		<td width="130">预计采购价 <input type="text" size="5" name="item[<?=$k?>][budget_price]" value="<?=isset($oi['budget_price']) ? $oi['budget_price'] : '' ?>" class="budget_price" /></td>
		<td width="300">预计供应商 
			<input type="text" size="4" disabled="disabled" class="budget_provider_id" value="<?=isset($oi['budget_provider_id']) ? $oi['budget_provider_id'] : ''?>"/>
			<input type="text" class="ajax_provider" value="<?=isset($oi['budget_provider_name']) ? $oi['budget_provider_name'] : ''?>" />
			<input type="hidden" class="budget_provider_id" name="item[<?=$k?>][budget_provider_id]" value="<?=isset($oi['budget_provider_id']) ? $oi['budget_provider_id'] : ''?>"/>
		</td>
		<td>
			原因 
			<select name="item[<?=$k?>][reason]">
				<option value="">请选择..</option>
				<?foreach (Q::ini('custom_flag/purchase_order_item_reason') as $por):?>
				<option value="<?=$por['value']?>"><?=$por['name']?></option>
				<?endforeach;?>
			</select>
		</td>
		<td>备注 <input type="text" size="40" name="item[<?=$k?>][notes]" value="<?=isset($oi['notes']) ? $oi['notes'] : ''?>" /></td>
		<td><a class="delete">删除</a></td>
	</tr>
</table>
<?endforeach;?>
</div>
<div align="left"><a id="add_item">添加产品</a></div>
<div align="center"><input type="button" class="button" id="add_save" value="保存采购单" /><input type="hidden" name="<?=Q::ini('unique_token_name');?>" value="<?=Helper_BSS_Normal::genToken();?>"/></div>
</form>
<?php $this->_endblock();?>